//
//  BasicWebViewController.m
//  AQUA
//
//  Created by 吕清毅 on 2018/8/8.
//  Copyright © 2018年 小河. All rights reserved.
//

#import "BasicWebViewController.h"

@interface BasicWebViewController ()<UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, assign) BOOL isSelct;
@end

@implementation BasicWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNavi];
    [self setWebView];
}

- (void)setNavi
{
    self.bgNavView.hidden = NO;
    self.bgNavView.backgroundColor = TotalWhite;
//    self.titLabel.text = [NSString stringWithFormat:@"%@",self.titleName];
}

- (void)setWebView
{
    self.webView = [UIWebView new];
    if (@available(iOS 11.0, *)) {
        self.webView.frame = CGRectMake(0, TopBarHeight_xs, SCREENWIDTH, SCREENHEIGHT-TopBarHeight_xs-BottomHeight_xs);
    } else {
        // Fallback on earlier versions
        self.webView.frame = CGRectMake(0, TopBarHeight, SCREENWIDTH, SCREENHEIGHT-TopBarHeight-BottomHeight);
    }
    self.webView.delegate = self;
    self.webView.userInteractionEnabled = YES;
    self.webView.backgroundColor = [UIColor whiteColor];
    
//    NSString *htmlString = [NSString stringWithFormat:@"<!doctype html><html><head><meta charset=utf-8' name='viewport' content='width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no'/></head><body>%@</body></html>",[NSString stringWithFormat:@"%@",self.url]];
//    [self.webView loadHTMLString:htmlString baseURL:nil];
    
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];
    [self.view addSubview:self.webView];
    self.webView.scalesPageToFit = YES;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    //获取所有的html
//    NSString *allHtml = @"document.documentElement.innerHTML";
    //获取网页title
    self.titLabel.text = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
//    NSString *htmlTitle = @"document.title";
//    //获取网页的一个值
//    NSString *htmlNum = @"document.getElementById('title').innerText";
//    //获取到得网页内容
//    NSString *allHtmlInfo = [self.webView stringByEvaluatingJavaScriptFromString:allHtml];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(nonnull NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *URL = request.URL;
    NSString *mainUrl = [URL absoluteString];
    //NSString *scheme = [URL scheme];
    if (![mainUrl isEqualToString:self.url])
    {
        if ([mainUrl rangeOfString:@"sign_contract_jump"].location != NSNotFound)
        {
            [self checkResult];
            return NO;
        }
    }
    return YES;
}

- (void)checkResult
{
    [[HttpRequest currentHttpRequest]postPath:CheckSignResult_API params:nil resultBlock:^(id Data, MsgModel *msgModel)
     {
         if (msgModel.status == 200)
         {
             CheckShopViewController *checkVC = [CheckShopViewController new];
             [self.navigationController pushViewController:checkVC animated:YES];
         }
     }];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    if ([error code] == NSURLErrorCancelled) {
        return;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
